package com.it.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/servletSession")
public class ServletSession extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletSession() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 第一次没有session 打印null ;
		HttpSession session = request.getSession();
		String value = (String) session.getAttribute("session_name");
		System.out.println("value:" + value);

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("session:" + value);

		// 向request中添加设置session 接下来打印设置的session
		session.setAttribute("session_name", " session_value");

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
